package com.xlongwei.todays.dao;

import java.util.Collections;
import java.util.List;

import org.noear.solon.Utils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Cache;
import org.noear.solon.data.annotation.CacheRemove;
import org.noear.solon.data.annotation.Transaction;
import org.noear.solon.data.sql.SqlBuilder;

import com.xlongwei.todays.config.MySqlUtils;
import com.xlongwei.todays.model.Todo;
import com.xlongwei.todays.view.TodoQuery;

@Component
public class TodoDao {
        @Inject
        MySqlUtils sqlUtils;

        @Transaction
        @CacheRemove(keys = "todo.${}", tags = "todos")
        public Integer create(Todo row) {
                assert row.getTodoid() == null;
                Object key = sqlUtils.sql("insert into t_todo(cron,title,status,next) values(?,?,?,?)",
                                row.getCron(), row.getTitle(), row.getStatus(), row.getNext()).updateReturnKey();
                return Integer.parseInt(key.toString());
        }

        @Cache(key = "todo.${todoid}")
        public Todo retrieve(Integer todoid) {
                return sqlUtils
                                .sql("select todoid,cron,title,status,next from t_todo where todoid=?", todoid)
                                .queryRow(Todo.class);
        }

        @Transaction
        @CacheRemove(keys = "todo.${row.todoid}", tags = "todos")
        public Integer update(Todo row) {
                assert row.getTodoid() != null;
                return sqlUtils.sql("update t_todo set cron=?,title=?,status=?,next=? where todoid=?",
                                row.getCron(), row.getTitle(), row.getStatus(), row.getNext(), row.getTodoid())
                                .update();
        }

        @Cache(tags = "todos")
        public List<Todo> list(TodoQuery query) {
                SqlBuilder sqlSpec = new SqlBuilder();
                sqlSpec.appendIf(Utils.isBlank(query.getRemark()),
                                "select t.todoid,cron,title,status,next from t_todo t where 1=1");
                sqlSpec.appendIf(Utils.isNotBlank(query.getRemark()),
                                "select t.todoid,cron,title,status,next from t_todo t left join t_extra e on t.todoid=e.todoid where 1=1");
                sqlSpec.appendIf(query.getTodoid() != null, " and t.todoid<?", query.getTodoid());
                sqlSpec.appendIf(Utils.isNotBlank(query.getStatus()), " and status=?", query.getStatus());
                sqlSpec.appendIf(Utils.isNotBlank(query.getNext()), " and next=?", query.getNext());
                sqlSpec.appendIf(Utils.isNotBlank(query.getTitle()), " and title like ?", "%" + query.getTitle() + "%");
                sqlSpec.appendIf(Utils.isNotBlank(query.getRemark()), " and e.remark like ?",
                                "%" + query.getRemark() + "%");
                sqlSpec.append(" order by t.todoid desc");
                sqlSpec.appendIf(query.getLimit() != null, " limit ?", query.getLimit());
                List<Todo> list = sqlUtils.sql(sqlSpec).queryRowList(Todo.class);
                return list != null ? list : Collections.emptyList();
        }
}
